package com.ujiuye.controller;

import com.ujiuye.pojo.Ordersetting;
import com.ujiuye.service.IOrdersettingService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.MyFileUtils;
import com.ujiuye.utils.Result;
import com.ujiuye.vo.DateVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员设置-比如设置今天可预约人数等信息 前端控制器
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private IOrdersettingService ordersettingService;

    @Value("${uploadPath}")
    private String path;

    @RequestMapping("/downloadexcel")
    public ResponseEntity<byte[]> downloadexcel() throws IOException {
        //将套餐信息写入excel文件中
        File file = new File(path+"ordersetting_template.xls");
        //下载到客户端电脑
        return MyFileUtils.download(file);
    }

    @RequestMapping("/upload")
    public Result uoload(@RequestParam("excelFile") MultipartFile multipartFile){
        //调用上传工具类,把文件上传至服务器
        File file = MyFileUtils.upload(multipartFile, path);
        //判断excel文件是否为空
        if (file==null){
            return new Result(false, MessageConstant.UPLOAD_FAIL);
        }
        //读取excel文件,获取数据
        List<Ordersetting> list = ExcelToList(file);
        if (list.size()==0){
            return new Result(false,"表格没有数据");
        }
        //将数据写入数据库中
        try {
            return ordersettingService.update(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }

    //读取excel文件
    public List<Ordersetting> ExcelToList(File file){
        List<Ordersetting> list = new ArrayList<>();
        try {
            FileInputStream inputStream = new FileInputStream(file);
            //获取工作簿
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //获取第一张表
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取有效数据
            int rowNumber = sheet.getPhysicalNumberOfRows();
            for (int i=1;i<rowNumber;i++){
                //获取第i行
                XSSFRow row = sheet.getRow(i);
                //获取第一个单元格
                Date date = row.getCell(0).getDateCellValue();
                //把date转化为localDate
                LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                //可预约人数
                double number = row.getCell(1).getNumericCellValue();
                Ordersetting ordersetting = new Ordersetting(localDate,(int)number);
                list.add(ordersetting);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/date/{dateTime}")
    public Result dateList(@PathVariable("dateTime") String datetime){
        try {
            List<DateVo> dateVoList = ordersettingService.listDate(datetime);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,dateVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PutMapping("/update/{date}/{number}")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    public Result update(@PathVariable("date") Date date,@PathVariable("number") Integer number){
        try {
            return ordersettingService.update(date, number);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

}

